#include "UdpServer.hpp"
#include <memory>
#include <cstdio>
#include <vector>
void Usage(std::string proc)
{
    std::cout << proc << "local_id local_port" << std::endl;
}   

std::vector<std::string> black_words = {
    "rm",
    "unlink",
    "cp",
    "mv",
    "chmod",
    "exit"
    "reboot",
    "helt",
    "shutdown"
};

std::string OnMessageDefault(std::string request)
{
    return request + "[haha, got you!!]";
}

bool SafeCheck(std::string command)
{
    for(auto &k :black_words)
    {
        auto pos = command.find(k);
        if(pos != std::string::npos) return false;
    }

    return true;
}

std::string ExecuteCommand(std::string command)
{
    if(SafeCheck(command)) return "bad man!!";

    std::cout << "get a message" << command << std::endl;
    FILE *fp = popen(command.c_str(), "r");
    if(fp == nullptr)
    {
        return "execute error, reason is unknown";
    }

    std::string response;
    char buffer[1024];
    while(true)
    {
        char *s = fgets(buffer, sizeof (buffer), fp);
        if(!s) break;
        else response += buffer;
    }
    pclose(fp);
    return response.empty() ? "success" : response;
}
int main(int argc, char* argv[])
{
   if(argc != 2)
   {
       Usage(argv[0]);
       return Usage_Err;
   }

//    std::string ip = argv[1];
   uint16_t port = std::stoi(argv[1]);
   std::unique_ptr<UdpServer> usvr = std::make_unique<UdpServer>(port);
   usvr->Init();
   usvr->Start();

   
   return 0;
}